window.onload=function(){
	getId("home").style.display="block";
    if(localStorage.house){
    	 getId("home").style.backgroundImage=Player[localStorage.house];
    }
    if(localStorage.player){
    	getClass("zhujue1").src=Player[localStorage.player];
    }
    if(localStorage.money){
    	getClass("money").innerHTML=localStorage.money+"两";
    }
    if(localStorage.button){
        var button1=localStorage.button;
        var button2=JSON.parse(button1);
    	for(var j=0;j<6;j++){
    	if(button2[j]==j){
    		document.getElementsByTagName("button")[j].index=1;
    		document.getElementsByTagName("button")[j].innerHTML="装备";
    		document.getElementsByTagName("button")[j].xndex=j;
	    document.getElementsByTagName("button")[j].onclick=function(){
	    	getClass("zhujue1").src=Player[this.xndex];
	    }
	    document.getElementsByTagName("button")[j].xdext=j;
    	}
    }
    	for(var j=6;j<12;j++){
    		if(button2[j]==j){
    			document.getElementsByTagName("button")[j].index=1;
    		document.getElementsByTagName("button")[j].innerHTML="装备";
    		document.getElementsByTagName("button")[j].xndex=j;
    		 document.getElementsByTagName("button")[j].onclick=function(){
	    	getId("home").style.backgroundImage=Player[this.xndex];
	    }
    		 document.getElementsByTagName("button")[j].xdext=j;
    		}
    	}
    
    console.log(localStorage);
    }
	var imgSrc={
		"accident":"img/accident.png",
		"guohe":"img/1.png",
		"lu":"img/youling.jpg",
		"fight1":"img/dazhan1.jpg",
		"fight2":"img/dazhan2.jpg",
		"youling1":"img/youling1.png",
		"youling2":"img/youling2.png",
		"youling3":"img/youling3.png",
		"youling4":"img/youling4.png"
	};
	var Player=[
		"img/laodaoshi.png",
		"img/pifu1.png",
		"img/pifu2.png",
		"img/pifu3.png",
		"img/pifu4.png",
		"img/pifu5.png",
		"url(img/fangzi.jpg)",
		"url(img/shinei1.jpg)",
		"url(img/shinei2.png)",
		"url(img/shinei3.jpg)",
		"url(img/shinei4.jpg)",
		"url(img/shinei5.jpg)"
	]
	var time=17;
	var ten=20;
	var a=[]; 
	var index=0;
	var accident=[];
	var lu=[];
	var gezi=-1;
	var warp=document.getElementById("warp");
	var move=warp.getElementsByClassName("move");
	var heliu=document.getElementsByClassName("heliu");
	var guohe=document.getElementsByClassName("guohe2");
	var player=warp.getElementsByClassName("player");
	var thing=document.getElementById("thing");
	var yugou=document.getElementsByClassName("yugou");
	var shibian=document.getElementsByClassName("shibian");
	var button=[];
	
	var Thing=function(width,height,x,y){
		this.width=width;
		this.height=height;
		this.x=x;
		this.y=y;
		this.imgNode=null;//代表<img>
	}
	Thing.prototype={
		init:function(imgSrcbullet){
			this.imgNode=document.createElement("img");
			this.imgNode.src=imgSrcbullet;
			this.imgNode.style.width=this.width+"px";
			this.imgNode.style.height=this.height+"px";
			this.imgNode.style.left=this.x+"px";
			this.imgNode.style.top=this.y+"px";
			this.imgNode.style.position="absolute";
			warp.appendChild(this.imgNode);
		}
	}
	console.log(parseInt(document.getElementsByTagName("p")[2].innerHTML));
	getClass("dangpu").onclick=function(){
		getClass("shangcheng").style.display="block";
	}
	getClass("youxiang").onclick=function(){
		startganshi();
	};
	getClass("return").onclick=function(){
		getClass("shangcheng").style.display="none";
	}
	getClass("save").onclick=function(){
		for(var i=6;i<12;i++){
		if(getId("home").style.backgroundImage==Player[i]){
			localStorage.house=i;
		}
	}
		for(var j=0;j<Player.length;j++){
			if(getClass("zhujue1").src==Player[j]){
				localStorage.player=j;
			}
		}
		for(var i=0;i<6;i++){
		if(document.getElementsByTagName("button")[i].index){
		    button[i]=i;
			var str=JSON.stringify(button);
			localStorage.button=str;
		}
		
		}
		for(var i=6;i<12;i++){
			if(document.getElementsByTagName("button")[i].index){
		    button[i]=i;
			var str=JSON.stringify(button);
			localStorage.button=str;
		}
		}
		localStorage.money=parseInt(getClass("money").innerHTML);
		getClass("save").innerHTML="保存成功";
		getClass("save").style.color="palevioletred";
		setTimeout(function(){
			getClass("save").innerHTML="保存进度";
			getClass("save").style.color="red";
		},3000);
		
	}
	for(var i=0;i<6;i++){
		document.getElementsByTagName("button")[i].a=i;
		if(document.getElementsByTagName("button")[i].xdext==undefined){
	document.getElementsByTagName("button")[i].onclick=function(){
		if(parseInt(getClass("money").innerHTML)>parseInt(document.getElementsByTagName("p")[this.a].innerHTML)){
		getClass("zhujue1").src=Player[this.a];
		getClass("money").innerHTML=parseInt(getClass("money").innerHTML)-parseInt(document.getElementsByTagName("p")[this.a].innerHTML)+"两";
	    document.getElementsByTagName("button")[this.a].innerHTML="装备";
	    document.getElementsByTagName("button")[this.a].onclick=function(){
	    	getClass("zhujue1").src=Player[this.a];
	    }
	    document.getElementsByTagName("button")[this.a].index=1;
		}
	}
	}
	}
	for(var i=6;i<12;i++){
		document.getElementsByTagName("button")[i].a=i;
		if(document.getElementsByTagName("button")[i].xdext==undefined){
	document.getElementsByTagName("button")[i].onclick=function(){
		if(parseInt(getClass("money").innerHTML)>parseInt(document.getElementsByTagName("p")[this.a].innerHTML)){
		getId("home").style.backgroundImage=Player[this.a];
		getClass("money").innerHTML=parseInt(getClass("money").innerHTML)-parseInt(document.getElementsByTagName("p")[this.a].innerHTML)+"两";
	    document.getElementsByTagName("button")[this.a].innerHTML="装备";
	    document.getElementsByTagName("button")[this.a].onclick=function(){
	    	getId("home").style.background=Player[this.a];
	    }
	    document.getElementsByTagName("button")[this.a].index=1;
		}
	}
	}
	}
	
	function startganshi(){
		appearaccident();
		judgeaccident();
		while(index>7){
		index=0;
		for(var i=0;i<accident.length;i++){
			warp.removeChild(accident[i].imgNode);
		}
		accident.splice(0,accident.length);
		appearaccident();
		judgeaccident();
	}
		makegezi();
		getId("home").style.left="-100%";
	}
	function appearaccident(){
	for(var i=0;i<7;i++){
	    accident[i]=new Thing(100,100,GetRandomNum(0,12)*100,GetRandomNum(0,5)*100);
		accident[i].init(imgSrc.accident);
	}
	}
	function overganshi(){
		for(var i=0;i<7;i++){
	    getId("warp").removeChild(accident[i].imgNode);
	}
		accident.splice(0,accident.length);
		for(var i=0;i<lu.length;i++){
		getId("warp").removeChild(lu[i].imgNode);
	}
	
		lu.splice(0,lu.length);
		console.log(lu);
		getId("home").style.left="0%";
		gezi=-1;
		index=0;
		ten=10;
		time=17;
		player[0].style.left="0px";
		player[0].style.top="500px";
	}
	
	function judgeaccident(){
	for(var i=0;i<accident.length;i++){
		for(var j=0;j<accident.length;j++){
			if(accident[i].imgNode.style.left==accident[j].imgNode.style.left&&accident[i].imgNode.style.top==accident[j].imgNode.style.top||
			accident[i].imgNode.style.top=="0px"&&accident[i].imgNode.style.left=="1200px"){
				index++;
			}
		}
	}
	}
	
	function makegezi(){
	for(var k=1;k<14;k++){
		for(var j=1;j<7;j++){
			gezi++;
		lu[gezi]=new Thing(100,100,(k-1)*100,(j-1)*100);
		lu[gezi].init(imgSrc.lu);
		lu[gezi].imgNode.style.borderRadius="50px";
		lu[gezi].imgNode.style.zIndex="2";
	}
	}
	lu[72].imgNode.src="img/zhongdian.png";
	}
	
	move[0].onclick=function(){
		if(time==0){
			ten=-10;
			over();
			getClass("over").innerHTML="很遗憾你没能按时将尸体送到目的地，，，，这对你的江湖声望产生了一定影响！！银两-10；";
		}
	    else if(player[0].offsetTop>20&&zhengchu(player[0].offsetTop,100)==1){
	    player[0].style.top=player[0].offsetTop-100+"px";
	    setTimeout(function(){
			for(var i=0;i<7;i++){
		if(collision(accident[i].imgNode,player[0])){
		a[i]();
	}}
		if(player[0].offsetLeft==1200&&player[0].offsetTop==0){
			over();
		}
			
			},400);
			time--;
	}
	}
	move[1].onclick=function(){
		if(time==0){
		  ten=-10;
			over();
			getClass("over").innerHTML="很遗憾你没能按时将尸体送到目的地，，，，这对你的江湖声望产生了一定影响！！银两-10；";
		}
		else if(player[0].offsetTop<500&&zhengchu(player[0].offsetTop,100)==1){
		player[0].style.top=player[0].offsetTop+100+"px";
		setTimeout(function(){
			for(var i=0;i<7;i++){
		if(collision(accident[i].imgNode,player[0])){
		a[i]();
	}}
		if(player[0].offsetLeft==1200&&player[0].offsetTop==0){
			over();
		}	
		},400);
		time--;
		}
	}
	move[2].onclick=function(){
		if(time==0){
			ten=-10;
			over();
			getClass("over").innerHTML="很遗憾你没能按时将尸体送到目的地，，，，这对你的江湖声望产生了一定影响！！银两-10；";
		}
		else if(player[0].offsetLeft<1200&&zhengchu(player[0].offsetLeft,100)==1){
		player[0].style.left=player[0].offsetLeft+100+"px";
		setTimeout(function(){
			for(var i=0;i<7;i++){
		if(collision(accident[i].imgNode,player[0])){
		a[i]();
	}}
		if(player[0].offsetLeft==1200&&player[0].offsetTop==0){
			over();
		}	
		},400);
		time--;
		}
	}
	move[3].onclick=function(){
		if(time==0){
			ten=-10;
			over();
			getClass("over").innerHTML="很遗憾你没能按时将尸体送到目的地，，，，这对你的江湖声望产生了一定影响！！银两-10；";
		}
		else if(player[0].offsetLeft>0&&zhengchu(player[0].offsetLeft,100)==1){
		player[0].style.left=player[0].offsetLeft-100+"px";
		setTimeout(function(){
			for(var i=0;i<7;i++){
		if(collision(accident[i].imgNode,player[0])){
		a[i]();
	}}
		if(player[0].offsetLeft==1200&&player[0].offsetTop==0){
			over();
		}	
		},400);
		time--;
	}
	}
	
	function zhengchu(x,y){
		var z=parseInt(x/y);
		if(z*y==x){
			return 1;
		}
		else
		return 0;
		
	}
	function collision(a,b){//两物体是否相撞
		if(a.offsetWidth+a.offsetLeft>b.offsetLeft&&a.offsetLeft<b.offsetLeft+b.offsetWidth&&
			 a.offsetHeight+a.offsetTop>b.offsetTop&&a.offsetTop<b.offsetHeight+b.offsetTop){
			return true;
		}
		else{
			return false;
		}
	}
	a[2]=function shibianThing(){
		allThing();
		ten=ten+5;
		shibian[0].style.display="block";
		document.getElementById("thingtextin").innerHTML="赶尸途中 ，突遇尸变，兀自默念：亡者路上，生人回避,你掏出了背上的长剑,一番大战";
		var dazhan1=new Thing(300,300,0,100);
			dazhan1.init(imgSrc.fight1);
		var dazhan2=new Thing(300,300,-600,100);
			dazhan2.init(imgSrc.fight2);
		setTimeout(function(){
			shibian[0].style.display="none";
			dazhan1.imgNode.style.zIndex="10";
			dazhan1.imgNode.style.transitionDuration="4s";
			dazhan1.imgNode.style.left=dazhan1.imgNode.offsetLeft+500+"px";
		},3000);
		setTimeout(function(){
			warp.removeChild(dazhan1.imgNode);
			dazhan2.imgNode.style.zIndex="10";
			dazhan2.imgNode.style.transitionDuration="6s";
			dazhan2.imgNode.style.left=dazhan2.imgNode.offsetLeft+1100+"px";
		},8000);
		setTimeout(function(){
			alloverThing();
			
			warp.removeChild(dazhan2.imgNode);
		},15000);
	}
	a[3]=function lanluThing(){
		allThing();
		ten=ten-10;
		getClass("lanlu").style.display="block";
		getId("thingtextin").innerHTML="赶尸途中，，突遇僵尸父子拦路，，，你权衡利弊，，掏出所有纸钱，洒向空中，，得以脱身 ";
		setTimeout(function(){ 
			alloverThing();
			getClass("lanlu").style.display="none";
		},5000);
		
	}
	a[5]=function youlingThing(){
		ten=ten+10;
		thing.style.display="block";
		var youling1=new Thing(100,100,-100,-100);
		youling1.init(imgSrc.youling1);
		youling1.imgNode.style.WebkitAnimation="youling1 6s";
		youling1.imgNode.style.zIndex="7";
		var youling2=new Thing(100,100,-100,400);
		youling2.init(imgSrc.youling2);
		youling2.imgNode.style.WebkitAnimation="youling2 6s";
		youling2.imgNode.style.zIndex="7";
		var youling3=new Thing(100,100,-100,700);
		youling3.init(imgSrc.youling3);
		youling3.imgNode.style.WebkitAnimation="youling3 6s";
		youling3.imgNode.style.zIndex="7";
		var youling4=new Thing(100,100,-100,400);
		youling4.init(imgSrc.youling4);
		youling4.imgNode.style.WebkitAnimation="youling4 6s";
		youling4.imgNode.style.zIndex="7";
		setTimeout(function(){
			getId("warp").removeChild(youling1.imgNode);
			getId("warp").removeChild(youling2.imgNode);
			getId("warp").removeChild(youling3.imgNode);
			getId("warp").removeChild(youling4.imgNode);
			getClass("youling").style.display="block";
			getId("thingtext").style.display="block";
			getId("thingtextin").innerHTML="荒野之中，偶遇阴魂环绕，，你于心不忍，，念起了往生咒，，，超度亡灵。。。"
			setTimeout(function(){
				getClass("youling").style.display="none";
			    getId("thingtext").style.display="none";
			    thing.style.display="none";
			},5000);
			
		},6500);
		
	}
	a[6]=function shanzeiThing(){
		thing.style.display="block";
		ten=ten-7;
		getClass("shanzei").style.display="block";
		getId("thingtext").style.display="block";
		getId("thingtextin").innerHTML="行知晋州，，惨遇山贼，你拔腿就跑，，最后被山贼捉住，狠揍了一顿，抢走身上财物。哭晕";
		var shanzei=new Thing(100,100,300,250);
		shanzei.init(imgSrc.guohe);
		shanzei.imgNode.style.zIndex="10";
		shanzei.imgNode.style.WebkitAnimation="taopao 2s";
		setTimeout(function(){
			thing.style.display="none";
			getClass("shanzei").style.display="none";
			getId("thingtext").style.display="block";
			getId("warp").removeChild(shanzei.imgNode);
		},5000)
		
	}
	a[4]=function fangliangThing(){
		allThing();
		ten=ten+5;
		getClass("fangliang").style.display="block";
		getId("thingtextin").innerHTML="路至通州府，，喜遇官府开仓放梁，，你免费吃了一顿午餐，心中甚喜。。";
		setTimeout(function(){ 
			alloverThing();
			getClass("fangliang").style.display="none";
		},5000);
		
	}
	a[1]=function yugouThing(){
		allThing();
		ten=ten-4;
		yugou[0].style.display="block";
		document.getElementById("thingtextin").innerHTML="夜间你围着篝火，吃着干粮，黑暗里走出一只野狗，对峙良久，你被抢走了晚餐，哭晕！！ ";
		setTimeout(function(){
			alloverThing();
			yugou[0].style.display="none";
		},5000);
		
	}
	
	a[0]=function accidentThing(){
		allThing();
		document.getElementById("thingtextin").innerHTML="你碰到了河流，船夫被你的尸体吓到要了三倍价钱，哭晕了 ";
		guohe[0].style.display="block";
		heliu[0].style.display="block";
		ten=ten-5;
	var guoheTimer=setInterval(function(){
		if(guohe[0].offsetLeft+guohe[0].offsetWidth-200>warp.getElementsByClassName("heliu")[0].offsetLeft){
		guohe[0].style.left=guohe[0].offsetLeft-100+"px";
		}
		else{
		guohe[0].style.left=guohe[0].offsetLeft+500+"px";
		alloverThing();
		clearInterval(guoheTimer);
		guohe[0].style.display="none";
		document.getElementsByClassName("heliu")[0].style.display="none";
		}
	},1000);
	}
	
	
	function GetRandomNum(Min,Max){//产生随机数
       var Range = Max - Min;   
       var Rand = Math.random();   
       return(Min + Math.round(Rand * Range));   
}   
    function allThing(){
    	player[0].style.display="none";
    	thing.style.display="block";
    	document.getElementById("thingtext").style.display="block";
    	for(i=0;i<4;i++){
			document.getElementsByClassName("move")[i].style.display="none";
		}
    }
    
    function alloverThing(){
    	document.getElementById("thingtext").style.display="none";
    	player[0].style.display="block";
    	thing.style.display="none";
    	for(i=0;i<4;i++){
			document.getElementsByClassName("move")[i].style.display="block";
		}
    }
    function over(){
    	getId("thing").style.display="block"
    	getClass("over").style.display="block";
    	getClass("over").innerHTML="你成功将尸体送到目的地，，雇主对你千恩万谢，并给了你十两银子做为报酬，，然而一路艰辛，，除去路上的花销，本次赶尸总共获得酬劳"
    	+ten+"两";
    	getClass("money").innerHTML=(parseInt(getClass("money").innerHTML)+ten)+"两";
    	setTimeout(function(){
    		overganshi();ten
    		getId("thing").style.display="none"
    	    getClass("over").style.display="none";
    	},5000);
    }
	function getId(index){
    	var xndex=document.getElementById(index);
    	return xndex;
    }
	function getClass(index){
		var a=document.getElementsByClassName(index)[0];
		return a;
	}
}



